package com.blog.backend.service.impl;

import com.blog.backend.config.CodeMsg;
import com.blog.backend.domain.BlogSummary;
import com.blog.backend.exception.GlobalException;
import com.blog.backend.mapper.BlogAccountMapper;
import com.blog.backend.mapper.BlogArticleMapper;
import com.blog.backend.mapper.FoodMapper;
import com.blog.backend.mapper.TravelManagerMapper;
import com.blog.backend.req.SettingReq;
import com.blog.backend.resp.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blog.backend.domain.BlogAccess;
import com.blog.backend.mapper.*;
import com.blog.backend.service.BlogIndexManagerService;
import com.blog.backend.utils.PageUtils;
import com.blog.backend.utils.TimeUtils;
import com.blog.backend.utils.UuidUtils;
import com.blog.backend.vo.PageRequest;
import com.blog.backend.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.blog.backend.utils.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class BlogIndexManagerServiceImpl implements BlogIndexManagerService {

    @Autowired
    private BlogArticleMapper blogArticleMapper;

    @Autowired
    private FoodMapper foodMapper;

    @Autowired
    private TravelManagerMapper travelManagerMapper;

    @Autowired
    private BlogAccountMapper blogAccountMapper;

    @Autowired
    private BlogAccessMapper blogAccessMapper;

    @Autowired
    private SysSettingMapper sysSettingMapper;

    @Resource
    private BlogSummaryMapper blogSummaryMapper;

    @Resource
    private BlogAlbumMapper blogAlbumMapper;

    @Resource
    private ImageMapper imageMapper;

    @Value("${weather}")
    private String key;
    @Override
    public StatisticsNumResp statisticsTotal() {
      Integer technologyNum = blogArticleMapper.statisticsTotal();
      Integer foodNum = foodMapper.statisticsTotal();
        Integer travelNum = travelManagerMapper.statisticsTotal();
        StatisticsNumResp statisticsNumResp = new StatisticsNumResp();
        statisticsNumResp.setTotalTechnology(technologyNum);
        statisticsNumResp.setTotalFood(foodNum);
        statisticsNumResp.setTotalTravel(travelNum);
        //获取评论总数
        Integer accessNums= blogAccessMapper.selectTotal();
        statisticsNumResp.setTotalAssess(accessNums);
        //获取访问总量
        Integer visitNums= sysSettingMapper.selectVisitNums();
        statisticsNumResp.setTotalVisited(visitNums);
        return statisticsNumResp;
    }

    @Override
    public PageResult<SysSettingResp> getSettingList(PageRequest request) {
        PageHelper.startPage(request.getPageNum(),request.getPageSize());
        List<SysSettingResp> list=blogAccountMapper.getSettingList();
        PageInfo<SysSettingResp> info=new PageInfo<SysSettingResp>(list);
        PageRequest pageRequest = new PageRequest();
        return PageUtils.getPageResult(pageRequest, info);
    }

    @Override
    public List<BlogAccess> selectTenAccess() {
        List<BlogAccess> list= blogAccessMapper.selectTenAccess();
        return list;
    }

    @Override
    public List<IndexArticleResp> selectHotArticle(Integer num) {
        List<IndexArticleResp> list = blogAccessMapper.selectHotArticle(num);
        List<IndexArticleResp> food =foodMapper.selectHotFood(num);
        list.addAll(food);
        List<IndexArticleResp> travel=travelManagerMapper.selectHotTravel(num);
        list.addAll(travel);
        return list;
    }

    @Override
    public DraftResp selectDraftNums() {
        DraftResp draftResp = new DraftResp();
        Integer articleDraft=blogArticleMapper.selectDraftNums();
        Integer foodDraft = foodMapper.selectDraftNums();
        Integer travelDraft = travelManagerMapper.selectDraftNums();
        draftResp.setFoodDraft(foodDraft);
        draftResp.setTechnologyDraft(articleDraft);
        draftResp.setTravelDraft(travelDraft);
        Integer totalAssess = this.getTodayAssess();
        draftResp.setAssessToday(totalAssess);
        return draftResp;
    }

    @Override
    public TitleDataResp selectTitleData() {
        //查询城市编号
        TitleDataResp resp = new TitleDataResp();
        String code= sysSettingMapper.selectCityCode();
        String s = HttpUtils.sendGet("https://restapi.amap.com/v3/weather/weatherInfo?city="+code+"&key="+key);
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONArray lives = (JSONArray) jsonObject.get("lives");
        JSONObject live = (JSONObject) lives.get(0);
        resp.setCity(live.getString("city"));
        resp.setProvince(live.getString("province"));
        resp.setTemperature(live.getString("temperature"));
        resp.setWeather(live.getString("weather"));
        //查询评论总数
        Integer integer = blogAccessMapper.selectTotal();
        resp.setAssessTotal(integer);
        resp.setTodayAssess(this.getTodayAssess());
        return resp;
    }

    @Override
    public SysSettingResp getSettingDetail(String settingId) {
        SysSettingResp resp=sysSettingMapper.getSettingDetail(settingId);
        return resp;
    }

    @Override
    public Boolean updateSettingDetail(SettingReq req) {
        sysSettingMapper.updateSettingDetail(req);
        return true;
    }

    @Override
    public PageResult<SummaryResp> getSummaryList(PageRequest request) {
        PageHelper.startPage(request.getPageNum(),request.getPageSize());
        List<SummaryResp> list= blogSummaryMapper.getSummaryList();
        PageInfo<SummaryResp> info=new PageInfo<SummaryResp>(list);
        PageRequest pageRequest = new PageRequest();
        return PageUtils.getPageResult(pageRequest, info);
    }

    @Override
    public Boolean addSummary(BlogSummary req) {
        req.setSummaryId(UuidUtils.getId());
        blogSummaryMapper.insertSelective(req);
        return true;
    }

    @Override
    public SummaryResp getSummaryDetail(String summaryId) {
        BlogSummary blogSummary = blogSummaryMapper.selectByPrimaryKey(summaryId);
        SummaryResp resp=new SummaryResp();
        BeanUtils.copyProperties(blogSummary,resp);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        resp.setCreateTimeStr(format.format(resp.getCreateTime()));
        return resp;
    }

    @Override
    public Boolean updateSummaryDetail(BlogSummary req) {
        blogSummaryMapper.updateByPrimaryKeySelective(req);
        return true;
    }

    @Override
    public Boolean deleteSummary(String summaryId) {
        BlogSummary blogSummary = blogSummaryMapper.selectByPrimaryKey(summaryId);
        if (blogSummary==null){
            throw new GlobalException(CodeMsg.FOOD_PARAMETER_ERROR);
        }
        blogSummaryMapper.updateSummaryStatus(summaryId);
        return true;
    }

    @Override
    public List<BlogAlbumResp> getIndexAlbums() {
        List<BlogAlbumResp> list=blogAlbumMapper.getIndexAlbums();
        for (BlogAlbumResp resp : list) {
            if (StringUtils.isNotBlank(resp.getAlbumPhotos())){
                List<PictureUrlResp> respList = imageMapper.selectImageList(resp.getAlbumPhotos());
                resp.setImageList(respList);
            }
        }
        return list;
    }

    @Override
    public BlogSummary getIndexSummary() {
        BlogSummary summary=  blogSummaryMapper.selectYearNew();
        return summary;
    }

    @Override
    public List<BlogAssessResp> getNewAssessList() {
        //查看最新的十条评论
        List<BlogAssessResp> list= blogAccessMapper.getNewAssessList();
        return list;
    }

    @Override
    public List<IndexArticleResp> getNewArticleList(int num) {
        List<IndexArticleResp> list = blogAccessMapper.getNewArticle(num);
        List<IndexArticleResp> food =foodMapper.getNewArticle(num);
        list.addAll(food);
        List<IndexArticleResp> travel=travelManagerMapper.getNewArticle(num);
        list.addAll(travel);
        return list;
    }

    @Override
    public BlogAlbumResp getPhotosByType(String type) {
        BlogAlbumResp resp= blogAlbumMapper.selectPhotosByType(type);
            if (resp!=null && StringUtils.isNotBlank(resp.getAlbumPhotos())){
                List<PictureUrlResp> respList = imageMapper.selectImageList(resp.getAlbumPhotos());
                resp.setImageList(respList);
            }

        return resp;
    }

    @Override
    public IndexArticleResp getNewTechnology() {
        IndexArticleResp resp=  blogArticleMapper.selectNewTechonoly();
        return resp;
    }

    private Integer getTodayAssess(){
        //查询每日评论总数
        //获取当日的开始时间
        Integer totalAssess= blogAccessMapper.selectTodayAssess(TimeUtils.getTodayBegin(),TimeUtils.getTodayEnd());
        return totalAssess;
    }
}
